/**
 * 
 */
package com.et114.modules.products.commons;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;

import com.et114.core.common.CONST;
import com.et114.core.utility.StringPool;
import com.et114.modules.products.base.ProImage;
import com.et114.modules.products.card.vo.Card;
import com.et114.modules.products.cellphone.vo.CellPhone;
import com.et114.modules.products.digitalTv.vo.DigitalTv;
import com.et114.modules.products.netbook.vo.NetBook;
import com.et114.modules.products.params.ParamType;
import com.et114.modules.products.params.ParamValue;
import com.et114.modules.products.proservices.ProService;
import com.et114.modules.products.proservices.ProServiceType;
import com.et114.modules.products.securitydoor.vo.SecurityDoor;

import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.products.vo.IPro;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*
 * @author guanhw
 */
public class PorJsonObj {
	

	public static JSONObject getProBaseJSONObject( IPro pro ) {
		JSONObject jsonObj = new JSONObject( ) ;
		jsonObj.element ( "proId" , pro.getProId ( ) );
		jsonObj.element ( "proTitle" , pro.getProTitle ( ) );
		
		ProSuppliers proSupplier = pro.getProSupplier ( ) ; 
		jsonObj.element ( "proSupplierId" , proSupplier.getProSupplierId ( ) == null ? "" : proSupplier.getProSupplierId ( ) );
		jsonObj.element ( "proSupplierImg" , proSupplier.getProSupplierImg ( ) );
		jsonObj.element ( "proSupplierName" , proSupplier.getProSupplierName ( ) );
		jsonObj.element ( "proOnOffStatus" , pro.getProOnOffStatus ( ) );	
		jsonObj.element ( "offTime" , pro.getOffTime ( ) );	
		jsonObj.element ( "subj" , pro.getSubj ( ) ) ; 
		if ( pro instanceof Card ) {
			Card card =  ( Card ) pro ; 
			jsonObj.element ( "cardImg" , card.getCardImg ( ) );
			jsonObj.element ( "cardTypeName" , card.getCardTypev ( ).getCardTypeName ( ) );
			jsonObj.element ( "cardServicePlanName" , card
					.getCardServicePlanv ( ).getCardServicePlanName ( ) );
			
		} else if ( pro instanceof CellPhone ) {
			CellPhone cellPhone = ( CellPhone ) pro ; 
			jsonObj.element ( "brandName" , cellPhone.getBrand ( ).getEgn ( ) );
			jsonObj.element ( "model" , cellPhone.getModel ( ) );
			jsonObj.element ( "price" , cellPhone.getPrice ( ) ) ; 
			jsonObj.element ( "disPrice" , cellPhone.getDisPrice ( ) ) ;
			
		
			//image
			List< ProImage > pis =  cellPhone.getProImages ( ) ; 
			if ( pis != null )
				for ( ProImage pi : pis ) 
					jsonObj.accumulate ( "prop" , pi.getProp ( ) ) ;
			
			List< ProImage > colors = cellPhone.getProImageColors ( ) ; 
			JSONObject jsonColorsObj = new JSONObject( ) ;
			for ( ProImage pi : colors )  {
				jsonColorsObj.element ( "color" , pi.getColor ( ) ) ; 
				jsonColorsObj.element ( "colorImage" , pi.getProp ( ) ) ; 
				jsonObj.accumulate ( "colors" , "[" + jsonColorsObj + "]" ) ;
			}
			
			List< String  > features =  cellPhone.getBaseFeatures ( ) ; 
			if ( features != null ) {
				for ( String feature : features )
					jsonObj.accumulate ( "features" , feature ) ; 
			}
			
		} else if ( pro instanceof NetBook ) {
			NetBook netBook = ( NetBook ) pro ; 
			jsonObj.element ( "brandName" , netBook.getBrand ( ).getEgn ( ) );
			jsonObj.element ( "model" , netBook.getModel ( ) );
			jsonObj.element ( "price" , netBook.getPrice ( ) ) ; 
			jsonObj.element ( "disPrice" , netBook.getDisPrice ( ) ) ;
			//image
			List< ProImage > pis =  netBook.getProImages ( ) ; 
			for ( ProImage pi : pis ) { 
				jsonObj.accumulate ( "prop" , pi.getProp ( ) ) ;
			}			
			List< ProImage > colors = netBook.getProImageColors ( ) ; 
			JSONObject jsonColorsObj = new JSONObject( ) ;
			for ( ProImage pi : colors )  {
				jsonColorsObj.element ( "color" , pi.getColor ( ) ) ; 
				jsonColorsObj.element ( "colorImage" , pi.getProp ( ) ) ; 
				jsonObj.accumulate ( "colors" , "[" + jsonColorsObj + "]" ) ;
			}
			List< String  > features =  netBook.getBaseFeatures ( ) ; 
			if ( features != null ) {
				for ( String feature : features )
					jsonObj.accumulate ( "features" , feature ) ; 
			}
			
		} else if ( pro instanceof DigitalTv ) {
			DigitalTv dt = ( DigitalTv ) pro ; 
			jsonObj.element ( "digitalTvTypeName" , ( dt.getDigitalTvSpeedType ( ).getDigitalTvTypeName ( ) ) ) ;
			jsonObj.element ( "price" , dt.getPrice ( ) ) ; 
		} else if ( pro instanceof SecurityDoor  ) {
			SecurityDoor sd = ( SecurityDoor ) pro ; 
			jsonObj.element ( "sdTypeName" , sd.getSsType ( ).getSdTypeName ( ) ) ; 
			jsonObj.element ( "price" , sd.getPrice ( ) ) ; 
		}
		
		return jsonObj ; 
	}
	

	
	public static JSONObject getProContentJSONObject ( String content ) {
		JSONObject jsonObj = new JSONObject ( );
		jsonObj.element ( "content" , content );
		return jsonObj;
		
	}	
	
	public static JSONObject getProSubjJSONObject ( IPro pro ) {
		JSONObject jsonObj = new JSONObject ( );
		jsonObj.element ( "subj" , pro.getSubj ( ) );
		return jsonObj;
	}	
	
	public static JSONObject getFeaturesJSONObject ( List< String > features  ) {
		JSONObject jsonObj = new JSONObject ( );
		for ( String feature : features ) 
			jsonObj.accumulate ( "features" , feature );
		return jsonObj;
	}		
	
	public static JSONObject error( String c , HttpServletResponse response , int errorCode ) throws Exception {
		JSONObject jsonObj = new JSONObject ( );
		if ( CONST.COUNTRY_ZH.equals ( c ) )
			jsonObj.element ( "error" , "操作失败!" );
		if ( CONST.COUNTRY_US.equals ( c ) )
			jsonObj.element ( "error" , "Oper !" );
		response.sendError ( errorCode ) ;
		return jsonObj ; 
	}	
	
	public static JSONObject error(  ) {
		JSONObject jsonObj = new JSONObject ( );
		jsonObj.element ( "error" , "error" );
		return jsonObj ; 
	}	


	
	public static JSONObject error( String c ) {
		JSONObject jsonObj = new JSONObject ( );
		/*if ( CONST.COUNTRY_ZH.equals ( c ) )
			jsonObj.element ( "error" , "操作失败!" );
		if ( CONST.COUNTRY_US.equals ( c ) )
			jsonObj.element ( "error" , "Oper !" );*/
		jsonObj.element ( "error" , c );
		return jsonObj ; 
	}
	
	public static JSONObject success( String c ) {
		JSONObject jsonObj = new JSONObject ( );
		jsonObj.element ( "success" , c );
		return jsonObj ; 
	}
	
	public static JSONObject proOffTime( String c ) { 
		JSONObject jsonObj = new JSONObject ( );
		if ( CONST.COUNTRY_ZH.equals ( c ) )
			jsonObj.element ( "error" , "此商品已下架!" );
		if ( CONST.COUNTRY_US.equals ( c ) )
			jsonObj.element ( "error" , "off!" );
		return jsonObj ; 
		
	}
	
	
	public static JSONObject getProParamsInfo ( List < ParamType > paramTypeRoots ,
			Map < String , String > keyMap , String COUNTRY ) throws Exception {
		JSONObject jsonObj = new JSONObject ( );
		String in = "" ; 
		
		for ( ParamType rootParamType : paramTypeRoots ) {
			JSONObject jsonObjPv = new JSONObject ( );
			if ( rootParamType.getWorkflg ( ) == Const.COMMON_STATUS_ENA ) {
				List< ParamType > paramTypes = rootParamType.getChilds ( ) ; 
				for ( ParamType paramType : paramTypes ) {
					in = in + paramType.getTypeId ( ) ;
					Byte showStyle = paramType.getShowStyle ( );
					
								
					if ( showStyle != null ) {
						List< ParamValue > paramValues = paramType.getParamValues ( ) ; 
						for ( ParamValue paramValue : paramValues ) {
							Long paramId = paramValue.getParamId ( ) ; 
							String paramKey = in + StringPool.DASH + paramId ;
							String paramEn = paramValue.getParamEnName ( ) ;
							String paramCn = paramValue.getParamName ( ) ; 
							if ( Const.COMMON_HTML_SELECT_M == showStyle ) {
								if ( keyMap.containsKey ( paramKey ) ) { 
									if ( CONST.COUNTRY_ZH.equals ( COUNTRY ) )
										jsonObjPv.element ( paramCn , "YES" );
									
									if ( CONST.COUNTRY_US.equals ( COUNTRY ) )
										jsonObjPv.element ( paramEn , "YES" );								
								}
								else {
									if ( CONST.COUNTRY_ZH.equals ( COUNTRY ) )
										jsonObjPv.element ( paramCn , "NO" );
									
									if ( CONST.COUNTRY_US.equals ( COUNTRY ) )
										jsonObjPv.element ( paramEn , "NO" );
								}
								
							} else if ( Const.COMMON_HTML_SELECT_S == showStyle ) {
								if ( CONST.COUNTRY_ZH.equals ( COUNTRY ) )
									jsonObjPv.element ( paramType.getTypeName ( ) ,
											paramCn );
								
								if ( CONST.COUNTRY_US.equals ( COUNTRY ) )
									jsonObjPv.element ( paramType.getTypeEnName ( ) ,
											paramEn );
							}
							else if ( Const.COMMON_HTML_SELECT_T == showStyle  ) {
								if ( keyMap.containsKey ( paramKey ) ) { 
									if ( CONST.COUNTRY_ZH.equals ( COUNTRY ) )
										jsonObjPv.element ( paramCn , keyMap
												.get ( paramKey ) );
									
									if ( CONST.COUNTRY_US.equals ( COUNTRY ) )
										jsonObjPv.element ( paramEn , keyMap
												.get ( paramKey ) );															
								}
							}
						}
					}
					in =  "";		
				}				
			}
			if ( CONST.COUNTRY_ZH.equals ( COUNTRY ) )
				jsonObj.element ( rootParamType.getTypeName ( ) , jsonObjPv ) ; 
			if ( CONST.COUNTRY_US.equals ( COUNTRY ) )
				jsonObj.element ( rootParamType.getTypeEnName ( ) , jsonObjPv ) ; 			
		}
		
		

		return jsonObj   ;
	}
	
	
	public static JSONObject getProSerivicesByTypeGroup( List< ProService >  types ,  List< ProService >  proSerivces ) throws Exception {
		JSONObject jsonObj = new JSONObject ( );
		JSONObject jsonPsObj = null;
		JSONObject jsonSubObj = null;
		JSONArray jsonSubArray = null;
		for ( ProService type : types ) {
			jsonPsObj = new JSONObject ( );
			jsonPsObj.element ( "typeName" , type.getProServiceType ( ).getProServiceName ( ) ) ;
			jsonPsObj.element ( "typeId" , type.getProServiceType ( ).getProServiceTypeId ( ) ) ;
			jsonSubArray = new JSONArray ( ); 
			for ( ProService proService : proSerivces ) {
				if ( type.getProServiceTypeId ( ).equals ( proService.getProServiceTypeId ( ) ) ) {
					
					jsonSubObj = new JSONObject( ) ; 
					jsonSubObj.element ( "proPlanName" , proService.getProPlanName ( ) );
					jsonSubObj.element ( "proServiceFeatures" , proService
							.getProServiceFeatures ( ) );
					jsonSubObj.element ( "proServiceAnytimeMinutes" , proService
							.getProServiceAnytimeMinutes ( ) );
					jsonSubObj.element ( "proServiceNightsWeekends" , proService
							.getProServiceNightsWeekends ( ) );
					jsonSubObj.element ( "proServiceMonthlyFee" , proService
							.getProServiceMonthlyFee ( ) );
					jsonSubObj.element ( "proServiceAdditionalLineMonthlyFee" ,
							proService.getProServiceAdditionalLineMonthlyFee ( ) );
					jsonSubArray.add ( jsonSubObj ) ; 
				}
			}
			jsonPsObj.element ( "ps" , jsonSubArray ) ;
			
			
		
			
			
			if ( types.size ( ) == 1 )
				jsonObj.accumulate ( "type" ,
						'[' + jsonPsObj.toString ( ) + ']' );
			else
				jsonObj.accumulate ( "type" , jsonPsObj );
		}
		return jsonObj;
	}	

	public static JSONArray getProSeriviceTypes( List< ProServiceType > psts ) throws Exception {
		//JSONObject jsonObj = new JSONObject ( );
		JSONObject jsonPsObj = new JSONObject ( );
		JSONArray jsonSubArray =  new JSONArray ( ); 
		for ( ProServiceType pst : psts ) {
			jsonPsObj.element ( "proServiceTypeId" , pst.getProServiceTypeId ( ) ) ; 
			jsonPsObj.element ( "proServiceName" , pst.getProServiceName ( ) ) ; 
			jsonPsObj.element ( "proServiceEnName" , pst.getProServiceEnName ( ) ) ; 
			jsonSubArray.add ( jsonPsObj ) ; 
		}
		//jsonObj.element ( "proServiceType" , jsonSubArray ) ;
		return jsonSubArray ; 
	}
	
	public static JSONObject getProSerivices( List< ProService >  proSerivces ) throws Exception {
		JSONObject jsonObj = new JSONObject ( );
		JSONObject jsonPsObj = null;
		for ( ProService proService : proSerivces ) {
			jsonPsObj = new JSONObject ( );
			jsonPsObj.element ( "proServiceId" , proService.getProServiceId ( ) );
			jsonPsObj.element ( "proPlanName" , proService.getProPlanName ( ) );
			jsonPsObj.element ( "proServiceFeatures" , proService
					.getProServiceFeatures ( ) );
			jsonPsObj.element ( "proServiceAnytimeMinutes" , proService
					.getProServiceAnytimeMinutes ( ) );
			jsonPsObj.element ( "proServiceNightsWeekends" , proService
					.getProServiceNightsWeekends ( ) );
			jsonPsObj.element ( "proServiceMonthlyFee" , proService
					.getProServiceMonthlyFee ( ) );
			jsonPsObj.element ( "proServiceAdditionalLineMonthlyFee" ,
					proService.getProServiceAdditionalLineMonthlyFee ( ) );
			ProServiceType  pst =  proService.getProServiceType ( ) ; 
			if ( pst != null ) {
				jsonPsObj.element ( "proServiceName" ,
						pst.getProServiceName ( ) );
				jsonPsObj.element ( "proServiceEnName" ,
						pst.getProServiceEnName ( ) );
				jsonPsObj.element ( "proServiceTypeId" ,
						pst.getProServiceTypeId ( ) );
			}
			
			if ( proSerivces.size ( ) == 1 )
				jsonObj.accumulate ( "ps" , '[' + jsonPsObj.toString ( ) + ']' );
			else
				jsonObj.accumulate ( "ps" , jsonPsObj );
		}
		return jsonObj;
	}
	
	

	
	public static ActionForward response ( HttpServletResponse response ,
			String jsonStr ) throws Exception {
		response.getWriter ( ).print ( jsonStr );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}

	public static ActionForward response ( HttpServletResponse response ,
			JSONObject jsonStr ) throws Exception {
		response.getWriter ( ).print ( jsonStr.toString ( ) );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
}
